package com.qf.cloud2203.service2.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.cloud2203.domain.entity.Vote;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/service2")
public class Service2Controller {

    @Value("${server.port}")
    private Integer port;

    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/home")
    public String home(){
        System.out.println("=============================");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "service2home:"+port;
    }

    public String fallback(){
        return "服务不可用";
    }


    @GetMapping("/findAll")
    public List<Vote> getVoteList(){

        final ArrayList<Vote> votes = new ArrayList<>();

        final Vote vote1 = new Vote();
        vote1.setId(1);
        vote1.setPhone("13566667777");
        vote1.setCandidateId(1);
        vote1.setVoteNum(6);

        votes.add(vote1);
        votes.add(vote1);

        return votes;

    }

    @PostMapping("/update")
    Vote updateVote(@RequestBody Vote vote){
        vote.setVoteNum(100);
        return vote;
    }

    @DeleteMapping("/delete/{id}")
    Vote deleteVote(@PathVariable("id") Integer id){
        System.out.println("delete vote id="+id);
        final Vote vote = new Vote();
        vote.setId(-1);
        return vote;
    }

}
